$(configdir)/l.conf: ./config/l.conf.tmpl
	@echo - $@
	@CONFIG_DIR="$(configdir)" META_DIR="$(metadir)" SRC_DIR="$(srcdir)" WWW_DIR="$(wwwdir)" \
	PHP_DIR="$(phpdir)" UPLOAD_DIR="$(uploaddir)" PID_FILE="$(pidfile)" \
	HTTP_PORT="$(http_port)" HTTPS_PORT="$(https_port)" SCGI_PORT="$(scgi_port)" \
	USER="$(user)" GROUP="$(group)" \
		envsubst '$${CONFIG_DIR} $${META_DIR} $${SRC_DIR} $${WWW_DIR}\
		$${PHP_DIR} $${UPLOAD_DIR} $${PID_FILE}\
		$${HTTP_PORT} $${HTTPS_PORT} $${SCGI_PORT}\
		$${USER} $${GROUP}' \
		< "$<" > "$@"

$(configdir)/ssl.conf: ./config/ssl.conf.tmpl
	@echo - $@
	@CONFIG_DIR="$(configdir)" META_DIR="$(metadir)" SRC_DIR="$(srcdir)" WWW_DIR="$(wwwdir)" \
	PHP_DIR="$(phpdir)" UPLOAD_DIR="$(uploaddir)" PID_FILE="$(pidfile)" \
	HTTP_PORT="$(http_port)" HTTPS_PORT="$(https_port)" SCGI_PORT="$(scgi_port)" \
	USER="$(user)" GROUP="$(group)" \
		envsubst '$${CONFIG_DIR} $${META_DIR} $${SRC_DIR} $${WWW_DIR}\
		$${PHP_DIR} $${UPLOAD_DIR} $${PID_FILE}\
		$${HTTP_PORT} $${HTTPS_PORT} $${SCGI_PORT}\
		$${USER} $${GROUP}' \
		< "$<" > "$@"

$(configdir)/php.ini: ./config/php.ini.tmpl
	@echo - $@
	@CONFIG_DIR="$(configdir)" META_DIR="$(metadir)" SRC_DIR="$(srcdir)" WWW_DIR="$(wwwdir)" \
	PHP_DIR="$(phpdir)" UPLOAD_DIR="$(uploaddir)" PID_FILE="$(pidfile)" \
	HTTP_PORT="$(http_port)" HTTPS_PORT="$(https_port)" SCGI_PORT="$(scgi_port)" \
	USER="$(user)" GROUP="$(group)" \
		envsubst '$${CONFIG_DIR} $${META_DIR} $${SRC_DIR} $${WWW_DIR}\
		$${PHP_DIR} $${UPLOAD_DIR} $${PID_FILE}\
		$${HTTP_PORT} $${HTTPS_PORT} $${SCGI_PORT}\
		$${USER} $${GROUP}' \
		< "$<" > "$@"

config_template: $(configdir)/l.conf $(configdir)/php.ini $(configdir)/ssl.conf

$(configdir)/mimetype.conf: ./config/mimetype.conf
	@echo - $@
	@cp "$<" "$@"

$(configdir)/rewrite.conf: ./config/rewrite.conf
	@echo - $@
	@cp "$<" "$@"

$(configdir)/ssl_cert.conf: ./config/ssl_cert.conf
	@echo - $@
	@cp "$<" "$@"

config_copy: $(configdir)/mimetype.conf $(configdir)/rewrite.conf $(configdir)/ssl_cert.conf

_config:
	# Config
	@mkdir -p "$(configdir)"

config: _config config_template config_copy

.PHONY: _config config
.PHONY: config_template config_copy
.PHONY: $(configdir)/l.conf $(configdir)/php.ini $(configdir)/ssl.conf
.PHONY: $(configdir)/mimetype.conf $(configdir)/rewrite.conf $(configdir)/ssl_cert.conf
